/**
 * Created by LeungZ on 2016/10/31.
 */
class AppSrv {
    constructor(commonSrv) {
        "ngInject";
        this._commonSrv = commonSrv;
    }

    // --------------------------------------构建------------------------------------
    build(para) {
        return this._commonSrv('post', '/ctg-imageconsole-web/build/buildImage', para);
    }

    upload(para) {
        var fd = new FormData();
        fd.append('index', para.index);
        fd.append('total', para.total);
        fd.append('fileName', para.fileName);
        fd.append('data', para.data);
        fd.append('timestampDirectory', para.timestamp);

        return this._commonSrv('formData', '/ctg-imageconsole-web/build/uploadFile', fd);
    }

    getBuildList(page, filter) {
        return this._commonSrv('get', '/ctg-imageconsole-web/build/queryBuildList', {
            pageNum: page.currentPage,
            pageSize: 10,
            imageName: filter && filter.name,
            version: filter && filter.ver,
            status: filter && filter.status
        });
    }

    getBuildLog(para) {
        return this._commonSrv('get', '/ctg-imageconsole-web/build/queryBuildLog', {
            dockerMicroImageId: para.id,
            lastTimeFileSize: para.size
        })
    }

    getBuildImages() {
        return this._commonSrv('get', '/ctg-imageconsole-web/imageRepository/microImages', {
            pageNum: 1,
            pageSize: 99
        })
    }

    //--------------------------------------发布------------------------------------
    getAppList(page, filter) {
        return this._commonSrv('get', '/ctg-imageconsole-web/dms/app/list', {
            pageNum: page.currentPage,
            pageSize: page.page || 10,
            appCode: filter && filter.name
        })
    }

    deleteApp(id) {
        return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/delete', {
            appId: id
        })
    }

    checkAppCode(appCode,namespace) {
        return this._commonSrv('get', '/ctg-imageconsole-web/dms/appCodeValidation', {
            appCode: appCode,
            namespace:namespace
        })
    }

    checkServiceCode(para) {
        return this._commonSrv('get', '/ctg-imageconsole-web/dms/serviceCodeValidation', {
            appId: para.appId,
            serviceCode: para.serviceCode
        })
    }

    getYaml(services) {
        var para = [];
        angular.forEach(services, function (service) {
            para.push({
                imageIndexId: service.baseCtrl.appImg.ver.id,
                serviceCode: service.baseCtrl.podAppName,
                groupCodeList: service.baseCtrl.groups,
                replicas: service.baseCtrl.podNum,
                cpu: service.baseCtrl.podCpu,
                memory: service.baseCtrl.podMem,
                volumeList: service.baseCtrl.volumes,
                volumeLogList: service.baseCtrl.logVolumes,
                namespace:service.namespace,
                clusterCode:service.clusterCode
            })
        });
        return this._commonSrv('post', '/ctg-imageconsole-web/dms/fixedJsonList', para)
    }

    releaseApp(para) {
        var paraData = {
            app: {
                appId: para.app.id,
                appCode: para.app.code,
                appName: para.app.name,
                description: para.app.desc,
                namespace:para.app.namespace
            },
            serviceList: [],
            versionList: []
        };
        angular.forEach(para.services, (ser, index)=> {
            paraData.serviceList.push({
                serviceCode: ser.baseCtrl.podAppName,
                url: ser.advCtrl && ser.advCtrl.podDomain
            });
            paraData.versionList.push(angular.extend({
                imageIndexId: ser.baseCtrl.appImg.ver.id,
                k8sCode: para.clusterCode,
                replicas: ser.baseCtrl.podNum,
                yamlContent: para.yamls[index],
                versionId: ser.rolVerId,
                namespace:ser.namespace//加上租户编码
            }, (ser.advCtrl && ser.advCtrl.atsCheck) ? {
                isAutoElastic: 1,
                maxPodNum: ser.advCtrl.maxPodNum,
                minPodNum: ser.advCtrl.minPodNum,
                elasticIndicator: ser.advCtrl.atsTarget,
                elasticStartTime: ser.advCtrl.atsStartTime,
                elasticEndTime: ser.advCtrl.atsEndTime,
                timeRate: ser.advCtrl.atsSpace,
                maxCpu: ser.advCtrl.atsLimit
            } : {
                isAutoElastic: 0
            }));
            console.log(ser.namespace)
        });

        if (para.services.length == 1 && para.services[0].versionId) {
            return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/version/update', angular.extend(paraData.versionList[0], {
                versionId: para.services[0].versionId
            }));
        }

        if (para.services.length == 1 && para.services[0].serviceId) {
            return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/version/createOrRollingUpdate', {
                service: {
                    serviceId: para.services[0].serviceId
                },
                version: paraData.versionList[0]
            });
        }

        if (para.app.id) {
            return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/create', {
                app: paraData.app,
                service: paraData.serviceList[0],
                version: paraData.versionList[0]
            });
        }

        return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/create', paraData);
    }

    getAppDetail(id) {
        return this._commonSrv('get', '/ctg-imageconsole-web/dms/app/detail', {
            appId: id
        })
    }

    update(para) {
        return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/updateDescription', {
            appId: para.id,
            description: para.content
        })
    }

    //------------------------------应用版本------------------------------------

    getServiceOfApp(para) {
        return this._commonSrv('get', '/ctg-imageconsole-web/dms/app/service/list', {
            appId: para.id,
            pageNum: para.currentPage,
            serviceId: para.serviceId,
            pageSize: 5
        })
    }

    getVerOfService(para, filter) {
        return this._commonSrv('get', '/ctg-imageconsole-web/dms/app/service/version/list', {
            serviceId: para.id,
            pageNum: para.currentPage,
            pageSize: para.pageSize,
            state: filter && filter.state
        })
    }

    getServiceDetail(id) {
        return this._commonSrv('get', '/ctg-imageconsole-web/dms/app/service/detail', {
            serviceId: id
        })
    }

    deleteService(id) {
        return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/delete', {
            serviceId: id
        })
    }

    getVerDetail(id) {
        return this._commonSrv('get', '/ctg-imageconsole-web/dms/app/service/version/detail', {
            versionId: id
        })
    }

    deleteVer(id,namespace) {
        return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/version/delete', {
            versionId: id,
            namespace:namespace
        })
    }

    stopVer(id,namespace) {
        return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/version/terminate', {
            versionId: id,
            namespace:namespace
        })
    }

    startVer(id,namespace) {
        return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/version/start', {
            versionId: id,
            namespace:namespace
        })
    }


    advUpdate(para, serId, verId) {
        if (para.currentType == 'domain') {
            return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/configLvs', {
                url: para.podDomain,
                serviceId: serId
            })
        }

        if (para.currentType == 'autoScale') {
            return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/version/configAutoScaler', para.atsCheck ? {
                isAutoElastic: 1,
                maxPodNum: para.maxPodNum,
                minPodNum: para.minPodNum,
                elasticIndicator: para.atsTarget,
                elasticStartTime: para.atsStartTime,
                elasticEndTime: para.atsEndTime,
                timeRate: para.atsSpace,
                maxCpu: para.atsLimit,
                versionId: verId
            } : {
                isAutoElastic: 0,
                versionId: verId
            });
        }
    }

    //--------------------------------------服务依赖------------------------------------

    getSerDepList(page, filter) {
        return this._commonSrv('get', '/ctg-imageconsole-web/serviceDependence/queryList', {
            pageNum: page.currentPage,
            pageSize: 10,
            name: filter && filter.name
        });
    }

    getAppsList(page, filter) {
        return this._commonSrv('get', '/ctg-imageconsole-web/serviceDependence/queryApps', {
            pageNum: page.currentPage,
            pageSize: 10,
            name: filter && filter.name
        });
    }

    getSerDepDetail(id) {
        return this._commonSrv('get', '/ctg-imageconsole-web/serviceDependence/queryDetail', {
            id: id
        })
    }

    saveSerDep(id, name, descJson) {
        return this._commonSrv('post', '/ctg-imageconsole-web/serviceDependence/save', {
            id: id,
            name: name,
            descJson: descJson

        })
    }

    deleteSer(id) {
        return this._commonSrv('post', '/ctg-imageconsole-web/serviceDependence/delete', {
            id: id
        })
    }
}
export default AppSrv